/*
 *   AS IS License
 * ==Naive Coders==
 */
package org.naive.metawatch.converter.cap;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.naive.metawatch.converter.parser.FieldSize;
import org.naive.metawatch.converter.parser.ParsedArray;
import org.naive.metawatch.converter.parser.cap.CapParseContext;
import org.naive.metawatch.converter.parser.cap.CapParseable;

/**
 *
 * @author gbu.000
 */
public class CapMethodComponent extends CapParseable { // second pass parsing ommits component header

    @ParsedArray(indexSize = FieldSize.BYTE, itemClass = CapExceptionHandlerInfo.class)
    public CapExceptionHandlerInfo[] exception_handlers;
    // manual second pass parsing
    public CapMethodInfo[] methods;
    private final CapClassDescriptorInfo[] classDescriptors;

    public CapMethodComponent(CapClassDescriptorInfo[] deskriptors) {
        classDescriptors = deskriptors;
    }

    @Override
    public void parse(CapParseContext px) throws ParseException {
        super.parse(px);
        final List<CapMethodInfo> ms = new ArrayList<>();
        for (CapClassDescriptorInfo cd : classDescriptors) {
            if (!cd.isInterface()) {
                for (CapMethodDescriptorInfo md : cd.methods) {
                    CapMethodInfo m = new CapMethodInfo(u(md.bytecode_count));
                    px.setOffset(u(md.method_offset));
                    m.parse(px);
                    ms.add(m);
                }
            }
        }
        methods = new CapMethodInfo[ms.size()];
        methods = ms.toArray(methods);
    }
}
